package com.eightywork.life.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.eightywork.life.R;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Telephone;

public class TelephoneListAdapter extends ArrayAdapter<Telephone> {
	private LayoutInflater mInflater;
	private int mResource = R.layout.item_telephone;
	private ImageLoader imageLoader;

	public TelephoneListAdapter(Context context, List<Telephone> list) {
		super(context, 0, list);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		View layout = convertView;
		if (layout == null) {
			layout = mInflater.inflate(mResource, parent, false);
		}
		final Telephone space = getItem(position);
		TextView title = (TextView) layout.findViewById(R.id.title);
		title.setText(space.getTitle());
		TextView telephone = (TextView) layout.findViewById(R.id.telephone);
		telephone.setText(space.getPhone());
		ImageView avatar = (ImageView) layout.findViewById(R.id.avatar);
		imageLoader.DisplayImage(space.getImage(), avatar, 0, 0, 0);

		ImageButton mobile = (ImageButton) layout
				.findViewById(R.id.button_moblie);
		mobile.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ space.getPhone()));
				getContext().startActivity(intent);
			}
		});
		return layout;
	}
}
